﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Web;

namespace Codes {
    public class Content {
        /// <summary>
        /// 发布内容
        /// </summary>
        /// <param name="group">分组</param>
        /// <param name="title">标题</param>
        /// <param name="content">内容</param>
        /// <param name="order">排序</param>
        /// <param name="author">作者</param>
        /// <param name="summary">摘要</param>
        /// <param name="cover">封面</param>
        /// <param name="label">标签</param>
        /// <returns></returns>
        public static bool add(string group, string title, string content, string order="0", string author="", string summary="", string cover="", string label="") {
            string sql="insert into Content([title],[author],[addtime],[click_pc],[summary],[content],[group],[cover],[order],[label],[click_wap])";
            sql+=" values(@title,@author,now(),0,@summary,@content,@group,@cover,@order,@label,0)";
            return Codes.AccessHelper.ExecuteNonQuery(connectionString.Content, sql, new OleDbParameter[]{
                new OleDbParameter("@title", title),
                new OleDbParameter("@author", author),
                new OleDbParameter("@summary", summary),
                new OleDbParameter("@content", content),
                new OleDbParameter("@group", group),
                new OleDbParameter("@cover", cover),
                new OleDbParameter("@order", order),
                new OleDbParameter("@label", label)
            })>0;
        }
        public static bool update(string ID, string title, string content, string order, string author, string summary, string cover, string label) {
            string sql="update Content set [title]=@title,[author]=@author,[summary]=@summary,[content]=@content,[cover]=@cover,[order]=@order,[label]=@label where ID=@id";
            return Codes.AccessHelper.ExecuteNonQuery(connectionString.Content, sql, new OleDbParameter[]{
                new OleDbParameter("@title", title),
                new OleDbParameter("@author", author),
                new OleDbParameter("@summary", summary),
                new OleDbParameter("@content", content),
                new OleDbParameter("@cover", cover),
                new OleDbParameter("@order", order),
                new OleDbParameter("@label", label),
                new OleDbParameter("@ID", ID)
            })>0;
        }
        public static bool changeOrder(int Order, int ID) {
            string sql="update Content set [order]=@Order where ID=@ID";
            return Codes.AccessHelper.ExecuteNonQuery(connectionString.Content, sql, new OleDbParameter[]{
                new OleDbParameter("@Order", Order),
                new OleDbParameter("@ID", ID)
            })>0;
        }
        public static bool deleteMass(string idList) {
            string sql="delete from Content where ID in("+idList+")";
            return Codes.AccessHelper.ExecuteNonQuery(connectionString.Content, sql)>0;
        }

        public static int getListCount(string group, string title) {
            string sql ="select count(1) from Content where 1=1 ";
            if(!string.IsNullOrEmpty(group)) {
                sql+=" and [group]="+group;
            }
            if(!string.IsNullOrEmpty(title)) {
                sql+=" and [title] like '%"+title+"%'";
            }
            return (int)Codes.AccessHelper.ExecuteScalar(connectionString.Content, sql);
        }
        public static System.Data.DataTable getList(int pageindex, int pagesize) {
            int notTop=((pageindex-1)*pagesize);
            string sql=string.Empty;
            if(notTop==0) {
                sql="select top "+pagesize+" * from Content order by [order] asc";
            } else {
                sql ="select top "+pagesize+" * from Content where id not in(select top "+((pageindex-1)*pagesize)+" id from Content) order by [order] asc";
            }
            return Codes.AccessHelper.ExecuteDataSet(connectionString.Content, sql).Tables[0];
        }
        public static System.Data.DataTable getList(string colm, string group, string title, int pageindex, int pagesize) {
            int notTop=((pageindex-1)*pagesize);
            string sql=string.Empty;
            if(notTop==0) {
                sql="select top "+pagesize+" "+colm+",(select title from ContentGroup where ContentGroup.id=[Content].[group]) as groupName from Content where 1=1 ";
                if(!string.IsNullOrEmpty(group)) {
                    sql+=" and [group]="+group;
                }
                if(!string.IsNullOrEmpty(title)) {
                    sql+=" and [title] like '%"+title+"%'";
                }
                sql+=" order by [order] asc";
            } else {
                string where="";
                if(!string.IsNullOrEmpty(group)) {
                    where+=" and [group]="+group;
                }
                if(!string.IsNullOrEmpty(title)) {
                    where+=" and [title] like '%"+title+"%'";
                }
                sql ="select top "+pagesize+" "+colm+",(select title from ContentGroup where ContentGroup.id=[Content].[group]) as groupName from Content where id not in(select top "+((pageindex-1)*pagesize)+" id from Content where 1=1 "+where+")  order by [order] asc";
            }
            return Codes.AccessHelper.ExecuteDataSet(connectionString.Content, sql).Tables[0];
        }

        public static System.Data.DataTable getInfo(string id) {
            string sql="select * from Content where id="+id;
            return Codes.AccessHelper.ExecuteDataSet(connectionString.Content, sql).Tables[0];
        }
    }
}